package com.ocom.payconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.entity.card.EntityCardRecord;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoCardRecord;
import com.ocom.payconfig.service.CardRecordService;
import com.ocom.payconfig.vo.GetCardRecordListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CardRecordServiceImpl extends ServiceImpl<IDaoCardRecord, EntityCardRecord> implements CardRecordService {

    @Autowired
    private IDaoCardRecord cardRecord;

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void AddCardInfo(Long comId, EntityCardRecord entityCardRecord) {
        cardRecord.insert(entityCardRecord);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public List<GetCardRecordListVo> cardOperDetailList(Long comId, JSONObject jsonObject) throws Exception{
        PageHelper.startPage(jsonObject.getInteger("page"),jsonObject.getInteger("size"));
        Long deptId = ObjectUtil.isEmpty(jsonObject.get("deptId"))?null:jsonObject.getLong("deptId");

        if (StringUtils.isNotEmpty(jsonObject.getString("endTime")) || StringUtils.isNotEmpty(jsonObject.getString("endTime"))){
            Date startTime= DateUtils.getDate(jsonObject.getString("startTime"),DateUtils.YYYY_MM_DD_HH_MM_SS);
            Date endTime= DateUtils.getDate(jsonObject.getString("endTime"),DateUtils.YYYY_MM_DD_HH_MM_SS);
            return cardRecord.cardOperDetailList(comId,startTime,endTime
                    ,jsonObject.getString("createUser"),jsonObject.getString("cardNo"),jsonObject.getInteger("operationType"),deptId);
        }else {
            return cardRecord.cardOperDetailList(comId,null,null
                    ,jsonObject.getString("createUser"),jsonObject.getString("cardNo"),jsonObject.getInteger("operationType"),deptId);
        }

    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public List<EntityCardRecord> countCardOperRecord(Long comId, JSONObject jsonObject) throws Exception{
        PageHelper.startPage(jsonObject.getInteger("page"),jsonObject.getInteger("size"));
        Long deptId = ObjectUtil.isEmpty(jsonObject.get("deptId"))?null:jsonObject.getLong("deptId");
        QueryWrapper<EntityCardRecord> queryWrapper=new QueryWrapper<>();
        if(ObjectUtil.isEmpty(deptId)){
            String[] str=new String[7];
            str[0]="COUNT(operation_type=1 or NULL)as issuingTimes";
            str[1]="COUNT(operation_type=3 or NULL)as checkoutTImes";
            str[2]="COUNT(operation_type=5 or NULL)as lossTimes";
            str[3]="COUNT(operation_type=6 or NULL)as unwindTimes";
            str[4]="SUM(cost_fee) as costFeeSum";
            str[5]="create_user";
            queryWrapper.eq("com_id",comId);
            queryWrapper.between("create_time",DateUtils.getDate(jsonObject.getString("startTime"),DateUtils.YYYY_MM_DD_HH_MM_SS),DateUtils.getDate(jsonObject.getString("endTime"),DateUtils.YYYY_MM_DD_HH_MM_SS));
            if (jsonObject.getString("createUser")!=null){
                queryWrapper.like("create_user",jsonObject.getString("createUser"));
            }
            if (jsonObject.getInteger("dateType")==1){
                str[6]="DATE_FORMAT(create_time, '%Y-%m-%d') as dayTime";
                queryWrapper.last("GROUP BY create_user,DATE_FORMAT(create_time, '%Y-%m-%d') order by  create_time asc");
            }else {
                str[6]="DATE_FORMAT(create_time, '%Y-%m') as dayTime";
                queryWrapper.last("GROUP BY create_user,DATE_FORMAT(create_time, '%Y-%m') order by  create_time asc");
            }
            queryWrapper.select(str);
            return cardRecord.selectList(queryWrapper);
        }else{
            //部门管理员
            queryWrapper.eq("cr.com_id",comId).eq("pi.dept_id",deptId);
            queryWrapper.between("cr.create_time",DateUtils.getDate(jsonObject.getString("startTime"),DateUtils.YYYY_MM_DD_HH_MM_SS),DateUtils.getDate(jsonObject.getString("endTime"),DateUtils.YYYY_MM_DD_HH_MM_SS));
            if (jsonObject.getString("createUser")!=null){
                queryWrapper.like("cr.create_user",jsonObject.getString("createUser"));
            }
            if (jsonObject.getInteger("dateType")==1){
                queryWrapper.last("GROUP BY cr.create_user,DATE_FORMAT(cr.create_time, '%Y-%m-%d') order by  cr.create_time asc");
            }else {
                queryWrapper.last("GROUP BY cr.create_user,DATE_FORMAT(cr.create_time, '%Y-%m') order by  cr.create_time asc");
            }
            return cardRecord.countCardOptRecord(queryWrapper);
        }

    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    public EntityCardRecord getRecodeInfo(Long comId, Long pId, String cardNo) {
        QueryWrapper<EntityCardRecord> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("p_id",pId);
        queryWrapper.eq("card_no",cardNo);
        queryWrapper.eq("operation_type",1);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }
}
